package net.stogi.dsper.ui.client.module.datasource;

import java.util.ArrayList;
import java.util.List;

import net.stogi.common.gwt.dispatch.client.core.ActionManager;
import net.stogi.common.gwt.frame.client.core.EventBus;
import net.stogi.common.gwt.frame.client.mvp.AbstractPresenter;
import net.stogi.common.mvp.core.behavior.Behavior;
import net.stogi.common.mvp.core.behavior.BehaviorWithValue;
import net.stogi.common.mvp.core.model.KeyValues;
import net.stogi.dsper.api.dto.DataSourceDto;
import net.stogi.dsper.ui.shared.module.datasource.ListDataSourcesAction;
import net.stogi.dsper.ui.shared.module.datasource.ListDataSourcesResult;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

class DataSourceListPresenter extends AbstractPresenter {

	private DataSourceListView dataSourceListView;

	private List<DataSourceDto> dataSourceListModel;

	private DataSourceDto selectedDataSourceModel;

	public DataSourceListPresenter(DataSourceListView dataSourceListView, ActionManager actionManager, EventBus eventBus) {
		super(actionManager, eventBus);
		this.dataSourceListView = dataSourceListView;
		initBehavior();
		initHandlers();
	}

	@Override
	protected void initBehavior() {
		dataSourceListView.setAddBehavior(new Behavior() {

			@Override
			public void behave() {
				dataSourceListView.block();
				eventBus.fireEvent(new OpenDataSourceEditorEvent(new DataSourceDto()));
			}
		});
		dataSourceListView.setCancelBehavior(new Behavior() {

			@Override
			public void behave() {
				dataSourceListView.disappear();
				eventBus.fireEvent(new DataSourceListClosedEvent());
			}
		});
		dataSourceListView.setEditBehavior(new Behavior() {

			@Override
			public void behave() {
				if (selectedDataSourceModel != null) {
					dataSourceListView.block();
					eventBus.fireEvent(new OpenDataSourceEditorEvent(selectedDataSourceModel));
				}
			}
		});
		dataSourceListView.setRowSelectedBehavior(new BehaviorWithValue<KeyValues<Long>>() {

			@Override
			public void behave(KeyValues<Long> value) {
				if (value != null) {
					selectedDataSourceModel = internalGetDataSourceFromModel(value.getKey());
				}
			}
		});
	}

	@Override
	protected void initHandlers() {
		eventBus.addHandler(DataSourceEditorClosedEvent.TYPE, new DataSourceEditorClosedHandler() {

			@Override
			public void onDataSourceEditorClosed(DataSourceEditorClosedEvent event) {
				if (event.isPersisted()) {
					executeListDataSources();
				}
				dataSourceListView.unblock();
			}
		});
		eventBus.addHandler(OpenDataSourceListEvent.TYPE, new OpenDataSourceListHandler() {

			@Override
			public void onOpenDataSourceList(OpenDataSourceListEvent event) {
				dataSourceListView.appear();
				executeListDataSources();
			}
		});
	}

	private void executeListDataSources() {
		actionManager.execute(new ListDataSourcesAction(), new AsyncCallback<ListDataSourcesResult>() {

			@Override
			public void onFailure(Throwable caught) {
				internalHandleException(caught);
			}

			@Override
			public void onSuccess(ListDataSourcesResult result) {
				dataSourceListModel = result.getDataSources();
				internalDisplayDataSources(dataSourceListModel);
			}
		});
	}

	private void internalDisplayDataSources(List<DataSourceDto> dataSourceList) {
		List<KeyValues<Long>> modelList = new ArrayList<KeyValues<Long>>();
		for (DataSourceDto dataSource : dataSourceList) {
			KeyValues<Long> model = new KeyValues<Long>(dataSource.getId());
			model.setValue("name", dataSource.getName());
			model.setValue("url", dataSource.getUrl());
			modelList.add(model);
		}
		dataSourceListView.displayDataSources(modelList);
	}

	private void internalHandleException(Throwable caught) {
		// TODO: Add some nice message module in order to show messages from
		// exceptions to user.
		GWT.log("execute*: fail", null);
	}

	private DataSourceDto internalGetDataSourceFromModel(Long id) {
		assert id != null : "Parameter 'id' can not be null";
		for (DataSourceDto dataSource : dataSourceListModel) {
			if (id.equals(dataSource.getId())) {
				return dataSource;
			}
		}
		return null;
	}
}
